package com.rightsidetech.http.controller.agentAdmin.mall;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.ModifyLog;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.mall.dto.request.AgentLackStockReq;
import com.rightsidetech.mall.dto.request.AgentPageReq;
import com.rightsidetech.mall.dto.request.CommodityAgentSortReq;
import com.rightsidetech.mall.dto.response.*;
import com.rightsidetech.mall.service.MallBrandService;
import com.rightsidetech.mall.service.MallCommodityService;
import com.rightsidetech.mall.service.MallTypeService;
import com.rightsidetech.system.service.SysAgentInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/agentAdmin/mall_commodity/")
@ShenyuSpringMvcClient(path = "/agentAdmin/mall_commodity/**")
@Api(tags = "代理商商品相关")
public class MallAgentCommodityController extends BaseController {

    @Resource
    private MallCommodityService mallCommodityService;

    @Resource
    private SysAgentInfoService sysAgentInfoService;

    @Resource
    private MallBrandService mallBrandService;

    @Resource
    private MallTypeService mallTypeService;

    @Log
    @Auth(bizType = Auth.AGENT)
    @Token
    @GetMapping("/page")
    @ApiOperation("分页查询代理商商品列表")
    public ResponseData<PageInfo<CommodityAgentResp>> page(AgentPageReq req){
        Long loginInfoId = Long.parseLong(getAttribute());
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(loginInfoId);
        if(agentInfoId == null){
            return ResponseData.fail("未找到代理商");
        }
        return mallCommodityService.agentPage(req,agentInfoId);
    }

    @Log
    @Auth(bizType = Auth.AGENT)
    @Token
    @GetMapping("/list")
    @ApiOperation("不分页代理商后台列表")
    @ApiOperationSupport(ignoreParameters = {"pageNo", "pageSize"})
    public ResponseData<List<CommodityAgentResp>> list(AgentPageReq req){
        Long loginInfoId = Long.parseLong(getAttribute());
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(loginInfoId);
        if(agentInfoId == null){
            return ResponseData.fail("未找到代理商");
        }
        return mallCommodityService.agentList(req,agentInfoId);
    }

    @Log
    @Auth(bizType = Auth.AGENT)
    @Token
    @GetMapping("/brand_list")
    @ApiOperation("品牌下拉选")
    public ResponseData<List<BrandListResp>> selectBrandList(@RequestParam(value = "brandName",required = false) String brandName){
        return mallBrandService.selectList(brandName);
    }

    @Log
    @Auth(bizType = Auth.AGENT)
    @Token
    @GetMapping("/type_list")
    @ApiOperation("分类下拉选")
    public ResponseData<List<TypePageResp>> selectTypeList(){
         return mallTypeService.getAll();
    }


    @Log
    @Auth(bizType = Auth.AGENT)
    @Token
    @GetMapping("/detail")
    @ApiOperation("商品详情")
    public ResponseData<CommodityDetailResp> findDetail(@RequestParam("commodityId") Long commodityId){
        Long loginInfoId = Long.parseLong(getAttribute());
        //获取登录账号代理商id
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(loginInfoId);
        if (StringUtils.isEmpty(agentInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        boolean check = mallCommodityService.checkCommodityByAgentId(agentInfoId, commodityId);
        if(!check){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"该商品未上架到此代理商");
        }
        ResponseData<CommodityDetailResp> result = mallCommodityService.findDetailById(commodityId);
        if(result.getCodeId() == 1){
            Byte status = result.getResData().getCommodityStatus();
            if (status != 3){
                return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"该商品还未上架");
            }
        }
        return result;
    }

    @Log
    @Auth(bizType = Auth.AGENT)
    @Token
    @PostMapping("/sort")
    @ApiOperation("代理商排序商品")
    @ModifyLog(serviceClass = MallCommodityService.class,idType = List.class,entityClass = MallAgentCommodityResp.class)
    public ResponseData sortCommodity(@RequestBody CommodityAgentSortReq req){
        Long loginInfoId = Long.parseLong(getAttribute());
        //获取登录账号代理商id
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(loginInfoId);
        if (StringUtils.isEmpty(agentInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        return mallCommodityService.agentSort(req,agentInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.AGENT)
    @PostMapping("/lack")
    @ApiOperation("代理商修改代理商商品状态")
    @ModifyLog(serviceClass = MallCommodityService.class,idType = List.class,entityClass = MallAgentCommodityResp.class)
    public ResponseData lackStock(@RequestBody AgentLackStockReq req){
        Long loginInfoId = Long.parseLong(getAttribute());
        //获取登录账号代理商id
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(loginInfoId);
        if (StringUtils.isEmpty(agentInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        return mallCommodityService.AgentLackStock(req,agentInfoId);
    }
}
